<template>
  <div>
    <el-dialog :title="this.ruleForm.id ? '修改' : '新增'" :visible="showDialog" width="30%" :before-close="handleClose"
      :close-on-click-modal="false">
      <div class="content">
        <div class="title">
          <svg-icon class="svg-icon" icon-class="infoAdd" />
          <span>押车人信息</span>
        </div>
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="120px" class="demo-ruleForm">
          <el-form-item label="押车人名称" prop="personName">
            <el-input v-model="ruleForm.personName" placeholder="请输入押车人名称"></el-input>
          </el-form-item>
          <el-form-item label="押车人电话" prop="phone" class="show_start">
            <el-input v-model="ruleForm.phone" placeholder="请输入押车人电话"></el-input>
          </el-form-item>
          <el-form-item label="填报人" prop="createUser">
            <el-input v-model="ruleForm.createUser" placeholder="请输入填报人" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="填报时间" prop="createTime">
            <el-date-picker style="width: 100%;" v-model="ruleForm.createTime" format="yyyy-MM-dd HH:mm:ss"
              value-format="yyyy-MM-dd HH:mm:ss" type="datetime" placeholder="请选择日期时间" :disabled="true">
            </el-date-picker>
          </el-form-item>
        </el-form>
      </div>
      <div class="dialog-footer-info">
        <el-button class="submit" @click="handleSubmit">确 认</el-button>
        <el-button class="back" @click="handleClose">返 回</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import rules from '@/utils/rules'
import { getUserKey } from '@/utils/auth'
import { formatDate } from '@/utils/methods'
import { escortPersonAddApi, escortPersonEditApi } from '@/api/info'
import { validatePhoneNumber } from '@/utils/validate'
export default {
  props: ['showDialog'],
  data() {
    const validatePhone = (rule, value, callback) => {
      if (value === '' || !validatePhoneNumber(value)) {
        callback(new Error('请输入正确的手机号'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        personName: '',
        phone: '',
        createUser: '',
        startTime: ''
      },
      rules: {
        ...rules,
        phone: [
          { validator: validatePhone, trigger: 'blur' }
        ],
      }
    };
  },
  methods: {
    handleClose() {
      this.$emit('update:showDialog', false)
      this.ruleForm = {
        personName: '',
        phone: '',
        createUser: '',
        createTime: ''
      }
      this.$refs.ruleForm.resetFields();
    },
    handleSubmit() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.ruleForm.id) {
            let { code } = await escortPersonEditApi(this.ruleForm)
            if (code == 0) {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
            }
          } else {
            let { code } = await escortPersonAddApi(this.ruleForm)
            if (code == 0) {
              this.$message({
                message: '添加成功',
                type: 'success'
              })
            }
          }
          this.$emit('handleRefresh')
          this.handleClose()
        } else {
          return false;
        }
      });
    },
    // 获取填报人时间
    handleGetInfo() {
      this.ruleForm.createUser = getUserKey() || 'nkjtcyf'
      this.ruleForm.createTime = formatDate(new Date())
    }
  }
}
</script>

<style lang='scss' scoped></style>
